package com.rex.service.impl;

import com.rex.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by frt on 2017/11/2.
 *  使用 JdbcTemplate 访问数据库
 */
@Service
public class UserService implements IUserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    @Transactional
    public void create(String name, Integer age) {
        jdbcTemplate.update("INSERT INTO USER(NAME, AGE) VALUES(?, ?)", name, age);
    }

    @Override
    @Transactional
    public void deleteByName(String name) {
        jdbcTemplate.update("DELETE FROM USER WHERE NAME = ?", name);
    }

    @Override
    public Integer getAllUsers() {
        return jdbcTemplate.queryForObject("SELECT COUNT(1) FROM USER", Integer.class);
    }

    @Override
    @Transactional
    public void deleteAllUsers() {
        jdbcTemplate.update("DELETE FROM USER");
    }
}
